//
//  HLMineViewController.m
//  HLAerospace
//
//  Created by hulian-ios on 17/8/29.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//

#import "HLMineViewController.h"
#import "HLSettingViewController.h"
#import "HLCollectViewController.h"
#import "HLMineNoteViewController.h"
#import "HLHistoryRecordController.h"
#import "HLVideoCacheController.h"
#import "HLUpdateUserNameController.h"
#import "HLUserInfo.h"

@interface HLMineViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UILabel *_userNameLabel;
}
@property(nonatomic , strong) UITableView * tableView ;

@end

@implementation HLMineViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setUI];
}

- (NSArray *)titles
{
    return @[@[@"离线缓存",@" 历史记录",@" 我的收藏",@" 我的笔记"],@[@" 设置"]];
}

- (NSArray *)images
{
    return @[@[@"离线缓存",@"历史记录",@"我的收藏",@"我的笔记"],@[@"设置"]];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)setUI
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];

    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
 
    [self.view addSubview:self.tableView];
    
    [self setHeaderView];
    
}

- (void)setHeaderView
{
    UIView *headerView = [[UIView alloc]init];
    headerView.frame = CGRectMake(0, 0, ScreenWidth, 0.3 * ScreenHeight);
    headerView.backgroundColor = kColorMineBlueBG;
    self.tableView.tableHeaderView = headerView;
    
    UIImageView *hederImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"头像"]];
    [hederImageView.image circleImage];
    hederImageView.center = CGPointMake(ScreenWidth * 0.5 , 0.15 * ScreenHeight);
    hederImageView.size = CGSizeMake(70, 70);
    [headerView addSubview:hederImageView];
    
    UILabel *userName = [UILabel new];
    userName.frame = CGRectMake(ScreenWidth * 0.25 , CGRectGetMaxY(hederImageView.frame) + (IS_IPHONE_5 ? 5 : 10), ScreenWidth * 0.5, 30);
    userName.text = [HLUserInfo account].nickname;
    userName.textAlignment = NSTextAlignmentCenter;
    userName.textColor = [UIColor whiteColor];
    userName.font = [UIFont boldSystemFontOfSize:16];
    _userNameLabel = userName;
    userName.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(updateUserName)];
    [userName addGestureRecognizer:tap];
    [headerView addSubview:userName];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return section == 0 ? 4 : 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ccellIdentifier"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:0 reuseIdentifier:@"ccellIdentifier"];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.textColor = kColorMainTitle;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text = [self titles][indexPath.section][indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[self images][indexPath.section][indexPath.row]];
        
    [tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:0];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 1 && indexPath.row == 0){
        HLSettingViewController *setting = [[HLSettingViewController alloc] init];
        [self.navigationController pushViewController:setting animated:YES];
    }
    
    if(indexPath.section == 0 && indexPath.row == 2){
        HLCollectViewController *collect = [[HLCollectViewController alloc] init];
        [self.navigationController pushViewController:collect animated:YES];
    }
    
    if(indexPath.section == 0 && indexPath.row == 3){
        HLMineNoteViewController *nineNote = [[HLMineNoteViewController alloc] init];
        [self.navigationController pushViewController:nineNote animated:YES];
    }
    
    if(indexPath.section == 0 && indexPath.row == 1){
        HLHistoryRecordController *history = [[HLHistoryRecordController alloc] init];
        [self.navigationController pushViewController:history animated:YES];
    }
    if(indexPath.section == 0 && indexPath.row == 0){
        HLVideoCacheController *cache = [[HLVideoCacheController alloc] init];
        [self.navigationController pushViewController:cache animated:YES];
    }
}

- (void)updateUserName
{
    HLUpdateUserNameController *up = [HLUpdateUserNameController new];
    up.userName = _userNameLabel.text;
    up.complainBlock = ^(NSString *usname){
        _userNameLabel.text = usname;
        
        /**** 重新归档 ****/
        HLUserLoginResult *result = [HLUserLoginResult new];
        result.userid = [HLUserInfo account].userid;
        result.nickname = usname;
        result.user_pic = [HLUserInfo account].user_pic;
        
        /**** 用户数据归档 ****/
        [HLUserInfo shared];
        [HLUserInfo saveAccount:result];
        
    };
    [self.navigationController pushViewController:up animated:YES];
}

@end
